import React, { Component } from 'react'
import './index.css'
import PropTypes from 'prop-types'

class Footer extends Component {
    static propTypes = {
        todoList: PropTypes.array.isRequired,
        checkedAll: PropTypes.func.isRequired,
        deleteDoneAll: PropTypes.func.isRequired
    }

    render() {
        const {todoList} = this.props
        const allTodo = todoList.length
        const countTodo = todoList.reduce((preV, currentV) => currentV.done ? preV + 1 : preV, 0)
        return (
            <div className="todo-footer">
                <label>
                    <input onChange={this.handleChange} checked={(countTodo === allTodo) && allTodo !== 0}
                           type="checkbox"/>
                </label>
                <span>
                    <span>已完成{countTodo}</span> / 全部{allTodo}
                </span>
                <button className="btn btn-danger" onClick={this.handelDeleteAll}>清除已完成任务</button>
            </div>
        )
    }

    handleChange = (e) => {
        this.props.checkedAll(e.target.checked)
    }

    handelDeleteAll = () => {
        this.props.deleteDoneAll()
    }
}

export default Footer
